<template>
  <div class="app-container">
    <el-form :inline="true" :model="queryParams" label-width="68px" ref="queryForm" size="small" v-show="showSearch">
      <el-form-item label="文章标题" prop="title">
        <el-input
          @keyup.enter.native="handleQuery"
          clearable
          placeholder="请输入文章标题"
          v-model="queryParams.title"
        />
      </el-form-item>
      <el-form-item label="文章类型" prop="typeId">
        <el-select
          clearable
          placeholder="文章类型"
          style="width: 240px"
          v-model="queryParams.typeId"
        >
          <el-option
            :key="type.id"
            :label="type.typeName"
            :value="type.id"
            v-for="type in typeList"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery" icon="el-icon-search" size="mini" type="primary">搜索</el-button>
        <el-button @click="resetQuery" icon="el-icon-refresh" size="mini">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <router-link :to="'/blog/articleAdd'">
          <el-button
            icon="el-icon-plus"
            plain
            size="mini"
            type="primary"
            v-hasPermi="['admin:article:add']"
          >新增
          </el-button>
        </router-link>
      </el-col>
      <el-col :span="1.5">
        <el-button
          :disabled="multiple"
          @click="handleDelete"
          icon="el-icon-delete"
          plain
          size="mini"
          type="danger"
          v-hasPermi="['admin:article:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          @click="handleExport"
          icon="el-icon-download"
          plain
          size="mini"
          type="warning"
          v-hasPermi="['admin:article:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table :data="articleList" @selection-change="handleSelectionChange" v-loading="loading">
      <el-table-column align="center" type="selection" width="55"/>
      <el-table-column align="center" label="id" prop="id"/>
      <el-table-column align="center" label="文章标题" prop="title" show-overflow-tooltip/>
      <el-table-column align="center" label="文章描述" prop="description" show-overflow-tooltip/>
      <el-table-column align="center" label="是否置顶" prop="isTop">
        <template slot-scope="scope">
          <el-switch
            @change="handleToTop(scope.row)"
            active-value="1"
            inactive-value="0"
            v-model="scope.row.isTop"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column align="center" label="阅读次数" prop="count"/>
      <el-table-column align="center" label="是否发布" prop="isRelease">
        <template slot-scope="scope">
          <el-switch
            @change="handleToTop(scope.row)"
            active-value="1"
            inactive-value="0"
            v-model="scope.row.isRelease"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column align="center" label="是否删除" prop="deleted">
        <template slot-scope="scope">
          <el-switch
            @change="handleToTop(scope.row)"
            active-value="1"
            inactive-value="0"
            v-model="scope.row.deleted"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column align="center" label="编辑时间" prop="updateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
        <template slot-scope="scope">
          <el-button
            @click="handleUpdate(scope.row)"
            icon="el-icon-edit"
            size="mini"
            type="text"
            v-hasPermi="['admin:article:edit']"
          >修改
          </el-button>
          <el-button
            @click="handleDelete(scope.row)"
            icon="el-icon-delete"
            size="mini"
            type="text"
            v-hasPermi="['admin:article:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      :limit.sync="queryParams.pageSize"
      :page.sync="queryParams.pageNum"
      :total="total"
      @pagination="getList"
      v-show="total>0"
    />
  </div>
</template>

<script>
  import { addArticle, delArticle, listArticle, updateArticle } from '@/api/blog/article'
  import { typeList } from '@/api/blog/type'

  export default {
    name: 'Article',
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 博客文章表格数据
        articleList: [],
        // 弹出层标题
        title: '',
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          title: null,
          typeId: null,
          tag: null,
          isTop: null
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          title: [
            { required: true, message: '文章标题不能为空', trigger: 'blur' }
          ],
          typeId: [
            { required: true, message: '文章类型id不能为空', trigger: 'blur' }
          ]
        },
        typeList: [],
        tagList: []
      }
    },
    created() {
      this.getList()
      this.getTypeList()
    },
    methods: {
      /** 查询博客文章列表 */
      getList() {
        this.loading = true
        listArticle(this.queryParams).then(response => {
          this.articleList = response.rows
          this.total = response.total
          this.loading = false
        })
      },
      getTypeList() {
        typeList().then(res => {
          this.typeList = res.rows
        })
      },
      // 取消按钮
      cancel() {
        this.open = false
        this.reset()
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          title: null,
          description: null,
          createdUserId: null,
          typeId: null,
          tag: null,
          markerdownMessage: null,
          createTime: null,
          updateTime: null,
          isTop: null,
          count: null,
          isRelease: null,
          deleted: null
        }
        this.resetForm('form')
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1
        this.getList()
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm('queryForm')
        this.handleQuery()
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset()
        this.open = true
        this.title = '添加博客文章'
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.$router.push({
          path: '/blog/articleEdit',
          query: {
            id: row.id
          }
        })
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs['form'].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateArticle(this.form).then(response => {
                this.$modal.msgSuccess('修改成功')
                this.open = false
                this.getList()
              })
            } else {
              addArticle(this.form).then(response => {
                this.$modal.msgSuccess('新增成功')
                this.open = false
                this.getList()
              })
            }
          }
        })
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids
        this.$modal.confirm('是否确认删除博客文章编号为"' + ids + '"的数据项？').then(function() {
          return delArticle(ids)
        }).then(() => {
          this.getList()
          this.$modal.msgSuccess('删除成功')
        }).catch(() => {
        })
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('admin/article/export', {
          ...this.queryParams
        }, `article_${new Date().getTime()}.xlsx`)
      },
      handleToTop(data) {
        updateArticle(data).then(res => {
          if (res.code === 200) {
            this.$modal.msgSuccess(res.msg)
            const that = this
            setTimeout(function() {
              that.getList()
            }, 500)
          }

        })
      }
    }
  }
</script>
